﻿
Namespace std
    Public Module conv
        Private Function set2backoperatingContainer(Of T As IComparable(Of T)) _
                                                   (ByVal s As [set](Of T), ByVal o As backoperatingContainer(Of T)) _
                                                   As Boolean
            If Not s Is Nothing AndAlso Not o Is Nothing Then
                o.clear()
                Dim it As [set](Of T).iterator = Nothing
                it = s.begin()
                While it <> s.end()
                    o.push_back(+it)
                    it += 1
                End While

                Return True
            Else
                Return False
            End If
        End Function

        Public Function set2vector(Of valueT As IComparable(Of valueT)) _
                                  (ByVal s As [set](Of valueT), ByRef rtn As vector(Of valueT)) As Boolean
            'save a call and leave rtn unchanged if s is nothing.
            If Not s Is Nothing Then
                If rtn Is Nothing Then
                    rtn = New vector(Of valueT)()
                End If

                Return set2backoperatingContainer(s, rtn)
            Else
                Return False
            End If
        End Function

        Private Function sortset2backoperatingContainer(Of T As IComparable(Of T)) _
                                                       (ByVal s As sortset(Of T), _
                                                        ByVal o As backoperatingContainer(Of T)) _
                                                       As Boolean
            If Not s Is Nothing AndAlso Not o Is Nothing Then
                o.clear()
                Dim it As sortset(Of T).iterator = Nothing
                it = s.begin()
                While it <> s.end()
                    o.push_back(+it)
                    it += 1
                End While

                Return True
            Else
                Return False
            End If
        End Function

        Public Function sortset2vector(Of valueT As IComparable(Of valueT)) _
                                      (ByVal s As sortset(Of valueT), ByRef rtn As vector(Of valueT)) As Boolean
            If Not s Is Nothing Then
                If rtn Is Nothing Then
                    rtn = New vector(Of valueT)()
                End If

                Return sortset2backoperatingContainer(s, rtn)
            Else
                Return False
            End If
        End Function

        Public Function sortset2vector(Of valueT As IComparable(Of valueT)) _
                                      (ByVal s As sortset(Of valueT)) As vector(Of valueT)
            Dim rtn As vector(Of valueT) = Nothing
            If sortset2vector(s, rtn) Then
                Return rtn
            Else
                Return Nothing
            End If
        End Function

        Public Function set2vector(Of valueT As IComparable(Of valueT)) _
                                  (ByVal s As [set](Of valueT)) As vector(Of valueT)
            Dim rtn As vector(Of valueT) = Nothing
            If set2vector(s, rtn) Then
                Return rtn
            Else
                Return Nothing
            End If
        End Function

        Public Function vector2set(Of valueT As IComparable(Of valueT)) _
                                  (ByVal s As vector(Of valueT), ByRef rtn As [set](Of valueT)) As Boolean
            If Not s Is Nothing Then
                If rtn Is Nothing Then
                    rtn = New [set](Of valueT)()
                Else
                    rtn.clear()
                End If
                Dim i As Int64
                For i = 0 To s.size() - 1
                    rtn.insert(s(i))
                Next

                Return True
            Else
                Return False
            End If
        End Function

        Public Function vector2sortset(Of valueT As IComparable(Of valueT)) _
                                      (ByVal s As vector(Of valueT), ByRef rtn As sortset(Of valueT)) As Boolean
            If Not s Is Nothing Then
                If rtn Is Nothing Then
                    rtn = New sortset(Of valueT)()
                Else
                    rtn.clear()
                End If
                Dim i As Int64
                For i = 0 To s.size() - 1
                    rtn.insert(s(i))
                Next

                Return True
            Else
                Return False
            End If
        End Function

        Public Function vector2set(Of valueT As IComparable(Of valueT)) _
                                  (ByVal s As vector(Of valueT)) As [set](Of valueT)
            Dim rtn As [set](Of valueT) = Nothing
            If vector2set(s, rtn) Then
                Return rtn
            Else
                Return Nothing
            End If
        End Function

        Public Function vector2sortset(Of valueT As IComparable(Of valueT)) _
                                      (ByVal s As vector(Of valueT)) As sortset(Of valueT)
            Dim rtn As sortset(Of valueT) = Nothing
            If vector2sortset(s, rtn) Then
                Return rtn
            Else
                Return Nothing
            End If
        End Function

        Public Function list2vector(Of v)(ByVal l As list(Of v), ByRef o As vector(Of v)) As Boolean
            If Not l Is Nothing Then
                If o Is Nothing Then
                    o = New vector(Of v)()
                Else
                    o.clear()
                End If

                Dim it As list(Of v).iterator = Nothing
                it = l.begin()
                While it <> l.end()
                    o.insert(+it)
                    it += 1
                End While

                Return True
            Else
                Return False
            End If
        End Function

        Public Function list2vector(Of v)(ByVal l As list(Of v)) As vector(Of v)
            Dim rtn As vector(Of v) = Nothing
            If list2vector(l, rtn) Then
                Return rtn
            Else
                Return Nothing
            End If
        End Function

        Public Function list2set(Of v As IComparable(Of v))(ByVal l As list(Of v), ByRef o As [set](Of v)) As Boolean
            If Not l Is Nothing Then
                If o Is Nothing Then
                    o = New [set](Of v)()
                Else
                    o.clear()
                End If

                Dim it As list(Of v).iterator = Nothing
                it = l.begin()
                While it <> l.end()
                    o.insert(+it)
                    it += 1
                End While

                Return True
            Else
                Return False
            End If
        End Function

        Public Function list2set(Of v As IComparable(Of v))(ByVal l As list(Of v)) As [set](Of v)
            Dim rtn As [set](Of v) = Nothing
            If list2set(l, rtn) Then
                Return rtn
            Else
                Return Nothing
            End If
        End Function

        Public Function set2list(Of v As IComparable(Of v))(ByVal s As [set](Of v), ByRef o As list(Of v)) As Boolean
            If Not s Is Nothing Then
                If o Is Nothing Then
                    o = New list(Of v)()
                End If

                Return set2backoperatingContainer(s, o)
            Else
                Return False
            End If
        End Function

        Public Function set2list(Of v As IComparable(Of v))(ByVal s As [set](Of v)) As list(Of v)
            Dim rtn As list(Of v) = Nothing
            If set2list(s, rtn) Then
                Return rtn
            Else
                Return Nothing
            End If
        End Function

#If RETIRED Then
        Public Function conv(Of T, c1 As container(Of T), c2 As container(Of T)) _
                            (ByVal i As c1, ByRef o As c2) As Boolean
            If o Is Nothing Then
                o = alloc(Of c2)()
            Else
                o.clear()
            End If
        End Function
#End If
    End Module
End Namespace
